package com.kcwl.carrier.domain.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kcwl.carrier.domain.entity.CarrierDriverInfo;
import com.kcwl.carrier.domain.repository.ICarrierDriverRepository;
import com.kcwl.carrier.infrastructure.enums.CarrierUseErrorCodeEnum;
import com.kcwl.carrier.infrastructure.enums.InviteStatusEnum;
import com.kcwl.carrier.infrastructure.exceptions.CarrierUserException;
import com.kcwl.carrier.infrastructure.repository.po.CarrierDriverPO;
import com.kcwl.carrier.interfaces.query.CarrierDriverQuery;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lxp
 * @date 2023/1/10 11:27
 * @Description
 */
@Service
public class CarrierDriverDomainService {

    @Autowired
    ICarrierDriverRepository carrierDriverRepository;

    public void updateCarrierDriverInfo(CarrierDriverInfo carrierDriverInfo) {
        CarrierDriverPO carrierDriverPO = carrierDriverRepository.getById(carrierDriverInfo.getId());
        if (carrierDriverPO == null) {
            throw new CarrierUserException(CarrierUseErrorCodeEnum.NOT_FOUND_CARRIER_DRIVER);
        }
        if (carrierDriverInfo.getDriverId() != null) {
            carrierDriverPO.setDriverId(carrierDriverInfo.getDriverId());
        }
        if (carrierDriverInfo.getIsDel() != null) {
            carrierDriverPO.setIsDel(carrierDriverInfo.getIsDel());
        }
        if (carrierDriverInfo.getInviteStatus() != null) {
            carrierDriverPO.setInviteStatus(carrierDriverInfo.getInviteStatus());
        }
        if (carrierDriverInfo.getInviteType() != null) {
            carrierDriverPO.setInviteType(carrierDriverInfo.getInviteType());
        }
        if (carrierDriverInfo.getUserId() != null) {
            carrierDriverPO.setUserId(carrierDriverInfo.getUserId());
        }
        if (carrierDriverInfo.getDriverCancelAuth() != null) {
            carrierDriverPO.setDriverCancelAuth(carrierDriverInfo.getDriverCancelAuth());
        }
        if (carrierDriverInfo.getDriverGrabAuth() != null) {
            carrierDriverPO.setDriverGrabAuth(carrierDriverInfo.getDriverGrabAuth());
        }
        if (carrierDriverInfo.getDriverConfirmAuth() != null) {
            carrierDriverPO.setDriverConfirmAuth(carrierDriverInfo.getDriverConfirmAuth());
        }
        if (carrierDriverInfo.getVehicleRange() != null) {
            carrierDriverPO.setVehicleRange(carrierDriverInfo.getVehicleRange());
        }
        carrierDriverPO.setUpdateTime(new Date());
        carrierDriverRepository.updateById(carrierDriverPO);
    }

    public List<CarrierDriverInfo> queryCarrierDriver(CarrierDriverQuery carrierDriverQuery) {
        QueryWrapper<CarrierDriverPO> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<CarrierDriverPO> lambdaQuery = Wrappers.lambdaQuery(CarrierDriverPO.class);
        if (carrierDriverQuery.getId() != null) {
            lambdaQuery.eq(CarrierDriverPO::getId, carrierDriverQuery.getId());
        }
        if (!CollectionUtil.isEmpty(carrierDriverQuery.getIds())) {
            lambdaQuery.in(CarrierDriverPO::getId, carrierDriverQuery.getIds());
        }
        if (carrierDriverQuery.getUserId() != null) {
            lambdaQuery.eq(CarrierDriverPO::getUserId, carrierDriverQuery.getUserId());
        }
        if (carrierDriverQuery.getIsDel() != null) {
            lambdaQuery.eq(CarrierDriverPO::getIsDel, carrierDriverQuery.getIsDel());
        }
        if (carrierDriverQuery.getDriverId() != null) {
            lambdaQuery.eq(CarrierDriverPO::getDriverId, carrierDriverQuery.getDriverId());
        }
        if (carrierDriverQuery.getInviteStatus() != null) {
            lambdaQuery.eq(CarrierDriverPO::getInviteStatus, carrierDriverQuery.getInviteStatus());
        }
        if (carrierDriverQuery.getInviteType() != null) {
            lambdaQuery.eq(CarrierDriverPO::getInviteType, carrierDriverQuery.getInviteType());
        }
        if (!CollectionUtil.isEmpty(carrierDriverQuery.getInviteStatusList())) {
            lambdaQuery.in(CarrierDriverPO::getInviteStatus, carrierDriverQuery.getInviteStatusList());
        }
        if (!CollectionUtil.isEmpty(carrierDriverQuery.getDriverIdList())) {
            lambdaQuery.in(CarrierDriverPO::getDriverId, carrierDriverQuery.getDriverIdList());
        }
        if (!CollectionUtil.isEmpty(carrierDriverQuery.getInviteTypes())) {
            lambdaQuery.in(CarrierDriverPO::getInviteType, carrierDriverQuery.getInviteTypes());
        }
        List<CarrierDriverPO> list = carrierDriverRepository.list(lambdaQuery);
        return KcBeanConverter.toList(list, CarrierDriverInfo.class);
    }

    public void createCarrierDriver(CarrierDriverInfo carrierDriverInfo) {
        CarrierDriverPO carrierDriverPO = KcBeanConverter.toBean(carrierDriverInfo, CarrierDriverPO.class);
        Date date = new Date();
        carrierDriverPO.setCreateTime(date);
        carrierDriverPO.setUpdateTime(date);
        carrierDriverPO.setIsDel(0);
        carrierDriverRepository.save(carrierDriverPO);
    }

    /**
     * 批量新增
     */
    public void batchCreateCarrierDriver(List<CarrierDriverInfo> carrierDriverInfoList) {
        carrierDriverRepository.saveBatch(KcBeanConverter.toList(carrierDriverInfoList, CarrierDriverPO.class));
    }

    /**
     * 查询用户下的所有司机id
     */
    public List<Long> queryDriverIdListByUserId(Long userId) {
        List<CarrierDriverPO> list = carrierDriverRepository.queryCarrierDriverListByUserId(userId);
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        List<Long> result = new ArrayList<>(list.size());
        for (CarrierDriverPO carrierDriverPO : list) {
            Integer inviteStatus = carrierDriverPO.getInviteStatus();
            Long driverId = carrierDriverPO.getDriverId();
            if (InviteStatusEnum.FLEET_ACCEPT.getCode() == inviteStatus || InviteStatusEnum.BE_ACCEPTED.getCode() == inviteStatus) {
                if (Objects.nonNull(driverId)) {
                    result.add(driverId);
                }
            }
        }
        return result;
    }

    public CarrierDriverInfo getById(Long id) {
        return KcBeanConverter.toBean(carrierDriverRepository.getById(id), CarrierDriverInfo.class);
    }
}
